#include "CMyListWidget.h"
#include "ui_CMyListWidget.h"

CMyListWidget::CMyListWidget(QWidget* parent)
    : QWidget(parent),
      ui(new Ui::CMyListWidget)
{
    ui->setupUi(this);

    initData();

    ui->listWidget->setMouseTracking(true);

    // list 信号转发
    connect(ui->listWidget, SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)), this, SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)));
    connect(ui->listWidget, SIGNAL(currentRowChanged(int)), this, SIGNAL(currentRowChanged(int)));
    connect(ui->listWidget, SIGNAL(currentTextChanged(const QString&)), this, SIGNAL(currentTextChanged(const QString&)));
    connect(ui->listWidget, SIGNAL(itemActivated(QListWidgetItem*)), this, SIGNAL(itemActivated(QListWidgetItem*)));
    connect(ui->listWidget, SIGNAL(itemChanged(QListWidgetItem*)), this, SIGNAL(itemChanged(QListWidgetItem*)));
    connect(ui->listWidget, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(slotItemClicked(QListWidgetItem*)));
    connect(ui->listWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SIGNAL(itemDoubleClicked(QListWidgetItem*)));
    connect(ui->listWidget, SIGNAL(itemEntered(QListWidgetItem*)), this, SIGNAL(itemEntered(QListWidgetItem*)));
    connect(ui->listWidget, SIGNAL(itemPressed(QListWidgetItem*)), this, SIGNAL(itemPressed(QListWidgetItem*)));
    connect(ui->listWidget, SIGNAL(itemSelectionChanged()), this, SIGNAL(itemSelectionChanged()));
}

CMyListWidget::~CMyListWidget()
{
    delete ui;
}

void CMyListWidget::addListWidgetItem(void* data, std::function<void(void* pData, QString& showText, QString& tipText)> showTextFunction, Qt::CheckState checkState)
{
    QString text, tipText;
    m_ShowTextFunction = showTextFunction;
    showTextFunction(data, text, tipText);

    CMyListWidgetItem* item = new CMyListWidgetItem(data);
    item->setText(text);
    if (!tipText.isEmpty()) {
        item->setToolTip(tipText);
    }
    item->setCheckState(checkState);
    item->setItemId(m_nCurrentCount++);
    ui->listWidget->addItem(item);

    if (checkState == Qt::Checked) {
        ++m_nSelectCount;
    }
    updateCount();
}

QListWidgetItem* CMyListWidget::getItem(int index)
{
    return ui->listWidget->item(index);
}

QListWidgetItem* CMyListWidget::getItem(const QPoint& point)
{
    return ui->listWidget->itemAt(point);
}

void* CMyListWidget::getItemData(int index)
{
    CMyListWidgetItem* item = dynamic_cast<CMyListWidgetItem*>(ui->listWidget->item(index));
    if (item) {
        return item->getItemData();
    }
    return nullptr;
}

void* CMyListWidget::getItemData(const QPoint& point)
{
    CMyListWidgetItem* item = dynamic_cast<CMyListWidgetItem*>(ui->listWidget->itemAt(point));
    if (item) {
        return item->getItemData();
    }
    return nullptr;
}

QModelIndex CMyListWidget::currentIndex()
{
    return ui->listWidget->currentIndex();
}

int CMyListWidget::getItemId(int index)
{
    CMyListWidgetItem* item = dynamic_cast<CMyListWidgetItem*>(ui->listWidget->item(index));
    if (item) {
        return item->getItemId();
    }
    return -1;
}

void CMyListWidget::clearItems()
{
    ui->listWidget->clear();
    initData();
}

int CMyListWidget::getItemCount()
{
    return ui->listWidget->count();
}

QVector<QListWidgetItem*> CMyListWidget::getCheckedItem()
{
    QVector<QListWidgetItem*> vItems;
    for (int i = 0; i < ui->listWidget->count(); i++) {
        QListWidgetItem* item = ui->listWidget->item(i);
        if (item->checkState() == Qt::Checked) {
            vItems.append(item);
        }
    }
    return vItems;
}

QVector<void*> CMyListWidget::getCheckedItemData()
{
    QVector<void*> vItems;
    for (int i = 0; i < ui->listWidget->count(); i++) {
        CMyListWidgetItem* item = (CMyListWidgetItem*)ui->listWidget->item(i);
        if (item->checkState() == Qt::Checked) {
            vItems.append(item->getItemData());
        }
    }
    return vItems;
}

int CMyListWidget::removeItem(int index)
{
    int nId = -1;
    CMyListWidgetItem* item = (CMyListWidgetItem*)ui->listWidget->item(index);
    if (item) {
        nId = item->getItemId();
        ui->listWidget->takeItem(index);
        updateCount();
    }
    return nId;
}

void CMyListWidget::updateShowText(int index)
{
    CMyListWidgetItem* item = (CMyListWidgetItem*)ui->listWidget->item(index);
    QString text, tipText;
    m_ShowTextFunction(item->getItemData(), text, tipText);
    item->setText(text);
    if (!tipText.isEmpty()) {
        item->setToolTip(tipText);
    }
}

void CMyListWidget::initData()
{
    m_nCurrentCount = 0;
    m_nSelectCount = 0;
}

void CMyListWidget::updateCount()
{
    ui->lbCount->setText(QString::number(m_nSelectCount));
}

void CMyListWidget::slotItemClicked(QListWidgetItem* item)
{
//    switch (item->checkState()) {
//        case Qt::Unchecked:
//            item->setCheckState(Qt::Checked);
//            ++m_nSelectCount;
//            break;

//        case Qt::Checked:
//            item->setCheckState(Qt::Unchecked);
//            --m_nSelectCount;
//            break;

//        default:
//            break;
//    }

//    updateCount();
    emit itemClicked(item);
}

void CMyListWidget::on_btnSelectAll_clicked()
{
    int nListCount = ui->listWidget->count();
    Qt::CheckState state = Qt::Unchecked;

    // 判断是全选还是清除所有
    if (m_nSelectCount == nListCount) {
        state = Qt::Unchecked;
        m_nSelectCount = 0;
    }
    else {
        state = Qt::Checked;
        m_nSelectCount = nListCount;
    }

    for (int i = 0; i < nListCount; ++i) {
        ui->listWidget->item(i)->setCheckState(state);
    }

    updateCount();
}
